package ua.pp.bizon.yandex.uploader.atom;

import org.apache.abdera.model.Element;

public abstract class Entry {

	private Element e;

	public Entry(Element e2) {
		this.e = e2;
	}

	public Element getElement() {
		return e;
	}

	protected void updateElement(Element element) {
		e = element;
	}

	public AppLink getLink(String name) {
		Element first = getFirst("link", "rel", name);
		return first == null ? null : new AppLink(first);
	}

	protected Element getFirst(String id) {
		for (Element e : this.e.getElements()) {
			if (e.getQName().getLocalPart().equals(id)) {
				return e;
			}
		}
		return null;
	}

	protected Element getFirst(String id, String type, String data) {
		for (Element e : this.e.getElements()) {
			if (e.getQName().getLocalPart().equals(id) && e.getAttributeValue(type) != null && e.getAttributeValue(type).equals(data)) {
				return e;
			}
		}
		return null;
	}

	public String getID() {
		return getFirst("id").getText();
	}

	@Override
	public String toString() {
		return "Entry:[" + e + "]";
	}
}